package org.yzhang3.data.utils;

import org.yzhang3.data.common.Constants;

public class ByteUtils {

	/**
	 * convert one byte to one unsigned int value.
	 * @param b
	 * @return
	 */
	public static int toUnsignedInt(byte b) {
		return b < 0 ? Math.abs(b) + 127 : b;
	}
	
	/**
	 * convert one byte to binary string(unsigned int).
	 * @param b
	 * @return
	 */
	public static String toBinary(byte b) {
		return IntegerUtils.to8Bits(toUnsignedInt(b));
	}
	
	/**
	 * convert one byte array to one unsigned int array.<br>
	 * <b>Notes: </b> <code>ints</code>'s length must greater or equal to <code>bs</code>'s length.
	 * @param bs
	 * @param ints
	 */
	public static int[] toUnsignedInt(byte[] bs) {
		int[] ints = new int[bs.length];
		for (int i=0; i<bs.length; i++) {
			ints[i] = toUnsignedInt(bs[i]);
		}
		return ints;
	}
	
	public static void main(String[] args) {
//		int alphabetSize = 256;
//		System.out.println(IntegerUtils.toByte(alphabetSize));
//		
//		if (Constants.isPrintEnable) {
//			System.out.println("Alphabet Size Bianry = " + ByteUtils.toBinary(IntegerUtils.toByte(alphabetSize)) + ", byte = " + IntegerUtils.toByte(alphabetSize));
//		}
//		
//		System.out.println(toUnsignedInt((byte) -128));
//		System.out.println(toUnsignedInt((byte) -127));
//		System.out.println(toUnsignedInt((byte) -126));
//		System.out.println(toUnsignedInt((byte) -1));
//		System.out.println(toUnsignedInt((byte) 127));
		
		byte b1 = (byte) -128;
		System.out.println((char) b1);
		System.out.println((char) 0);
		System.out.println((char) 255);
		System.out.println((char) 256);
		
		System.out.println(Character.digit((char) 255, 2));
		System.out.println(Character.digit((char) b1, 2));
		
	}
	
}
